package me.amiee.nicetab.demo.slice;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.enums.PopupAnimation;
import com.lxj.xpopup.interfaces.OnSelectListener;
import me.amiee.nicetab.demo.MainAbility;
import me.amiee.nicetab.demo.PagerProvider;
import me.amiee.nicetab.demo.ResourceTable;
import me.amiee.nicetab.library.NiceTabLayout;
import me.amiee.nicetab.library.SamplePagerItem;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    NiceTabLayout tabLayout;
    PageSlider pageSlider;
    List<SamplePagerItem> mTabs=new ArrayList<>();
    Text ysgdms,xsms,hzsx,xsyc,xx;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        ysgdms= (Text) findComponentById(ResourceTable.Id_ysgdms);
        xsms= (Text) findComponentById(ResourceTable.Id_xsms);
        hzsx= (Text) findComponentById(ResourceTable.Id_hzsx);
        xsyc= (Text) findComponentById(ResourceTable.Id_xsyc);
        xx= (Text) findComponentById(ResourceTable.Id_xx);
        tabLayout= (NiceTabLayout) findComponentById(ResourceTable.Id_nicetab);
        pageSlider= (PageSlider) findComponentById(ResourceTable.Id_pageSlider);
        mTabs.add(new SamplePagerItem("首页", // Title
                ResourceTable.Media_ic_home_selected, // Icon
                ResourceTable.Media_ic_home_default, // Icon
                ResourceTable.Color_home, // Indicator color
                ResourceTable.Color_home // Divider color
        ));

        mTabs.add(new SamplePagerItem("动态", // Title
                ResourceTable.Media_ic_activity_selected, // Icon
                ResourceTable.Media_ic_activity_default, // Icon
                ResourceTable.Color_activity, // Indicator color
                ResourceTable.Color_activity // Divider color
        ));

        mTabs.add(new SamplePagerItem("搜索", // Title
                ResourceTable.Media_ic_search_selected, // Icon
                ResourceTable.Media_ic_search_default, // Icon
                ResourceTable.Color_search, // Indicator color
                ResourceTable.Color_search // Divider color
        ));

        mTabs.add(new SamplePagerItem("我的", // Title
                ResourceTable.Media_ic_me_selected, // Icon
                ResourceTable.Media_ic_me_default, // Icon
                ResourceTable.Color_me, // Indicator color
                ResourceTable.Color_me // Divider color
        ));

        mTabs.add(new SamplePagerItem("长长长标题", // Title
                ResourceTable.Media_ic_coffee_selected, // Icon
                ResourceTable.Media_ic_coffee_default, // Icon
                ResourceTable.Color_coffee, // Indicator color
                ResourceTable.Color_coffee // Divider color
        ));
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(mTabs);
        pageSlider.setProvider(myPagerAdapter);
        ScrollView scrollView6 = (ScrollView) findComponentById(ResourceTable.Id_sc6);
        tabLayout.setScrollView(scrollView6);
        tabLayout.setViewPager(pageSlider, mTabs);
        tabLayout.setTabWidth(120);
        initLisenner();
    }
    private void initLisenner(){
        //颜色过渡模式
        ysgdms.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new XPopup.Builder(getContext())
                        .isDarkTheme(true)
                        .popupAnimation(PopupAnimation.ScaleAlphaFromRightTop)
                        .asCenterList("请选择一项", new String[]{"无", "默认选中", "相邻选中"},
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        showToast(text);
                                    }
                                })
                        .show();
            }
        });
        //显示模式
        xsms.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new XPopup.Builder(getContext())
                        .isDarkTheme(true)
                        .popupAnimation(PopupAnimation.ScaleAlphaFromRightTop)
                        .asCenterList("请选择一项", new String[]{"只显示标题", "只显示图标", "显示标题和图标"},
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        switch (text){
                                            case "只显示标题":
                                                tabLayout.setShowtype(NiceTabLayout.SHOWTYPE.Title);
                                                break;
                                            case "只显示图标":
                                                tabLayout.setShowtype(NiceTabLayout.SHOWTYPE.Icon);
                                                break;
                                            case "显示标题和图标":
                                                tabLayout.setShowtype(NiceTabLayout.SHOWTYPE.IconAndTitle);
                                                break;
                                        }
                                    }
                                })
                        .show();
            }
        });
        hzsx.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new XPopup.Builder(getContext())
                        .isDarkTheme(true)
                        .popupAnimation(PopupAnimation.ScaleAlphaFromRightBottom)
                        .asCenterList("请选择一项", new String[]{"指示条 底线 分割线", "指示条 分割线 底线", "底线 指示条 分割线"
                                        , "底线 分割线 指示条", "分割线 指示条 底线", "分割线 底线 指示条"},
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        //todo something
                                    }
                                })
                        .show();
            }
        });
        xsyc.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new XPopup.Builder(getContext())
                        .isDarkTheme(true)
                        .popupAnimation(PopupAnimation.ScaleAlphaFromRightBottom)
                        .asCenterList("请选择一项", new String[]{"显示底线", "隐藏底线", "显示分割线"
                                        , "隐藏分割线", "显示指示条", "隐藏指示条"},
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        switch (text){
                                            case "显示底线":
                                                tabLayout.setUnderlineHeight(1.5f);
                                                break;
                                            case "隐藏底线":
                                                tabLayout.setUnderlineHeight(0);
                                                break;
                                            case "显示分割线":
                                                tabLayout.setDividerWidth(1f);
                                                break;
                                            case "隐藏分割线":
                                                tabLayout.setDividerWidth(0);
                                                break;
                                            case "显示指示条":
                                                tabLayout.setIndicatorHeight(2f);
                                                break;
                                            case "隐藏指示条":
                                                tabLayout.setIndicatorHeight(0);
                                                break;
                                        }
                                    }
                                })
                        .show();
            }
        });
        xx.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new XPopup.Builder(getContext())
                        .isDarkTheme(true)
                        .popupAnimation(PopupAnimation.ScaleAlphaFromRightBottom)
                        .asCenterList("请选择一项", new String[]{"Tab布局", "Tab选中位置", "Tab小圆点"
                                        , "Tab图标风格"},
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        switch (text){
                                            case "Tab布局":
                                                new XPopup.Builder(getContext())
                                                        .isDarkTheme(true)
                                                        .popupAnimation(PopupAnimation.ScaleAlphaFromRightBottom)
                                                        .asCenterList("请选择一项", new String[]{"自适应宽度", "等宽显示"},
                                                                new OnSelectListener() {
                                                                    @Override
                                                                    public void onSelect(int position, String text) {
                                                                        switch (text){
                                                                            case "自适应宽度":
                                                                                tabLayout.setTabWidth(-1);
                                                                                break;
                                                                            case "等宽显示":
                                                                                tabLayout.setTabWidth(120);
                                                                                break;
                                                                        }
                                                                    }
                                                                })
                                                        .show();
                                                break;
                                            case "Tab选中位置":
                                                new XPopup.Builder(getContext())
                                                        .isDarkTheme(true)
                                                        .popupAnimation(PopupAnimation.ScaleAlphaFromRightBottom)
                                                        .asCenterList("请选择一项", new String[]{"左边", "居中"},
                                                                new OnSelectListener() {
                                                                    @Override
                                                                    public void onSelect(int position, String text) {
                                                                        switch (text){
                                                                            case "左边":
                                                                                tabLayout.setScrollchoosetype(NiceTabLayout.SCROLLCHOOSETYPE.Left);
                                                                                break;
                                                                            case "居中":
                                                                                tabLayout.setScrollchoosetype(NiceTabLayout.SCROLLCHOOSETYPE.Center);
                                                                                break;
                                                                        }
                                                                    }
                                                                })
                                                        .show();
                                                break;
                                            case "Tab小圆点":
                                                new XPopup.Builder(getContext())
                                                        .isDarkTheme(true)
                                                        .popupAnimation(PopupAnimation.ScaleAlphaFromRightBottom)
                                                        .asCenterList("请选择一项", new String[]{"测试小圆点", "清空小圆点"},
                                                                new OnSelectListener() {
                                                                    @Override
                                                                    public void onSelect(int position, String text) {
                                                                        switch (text){
                                                                            case "测试小圆点":
                                                                                tabLayout.showMsg(0, 1);
                                                                                tabLayout.showMsg(1, 12);
                                                                                tabLayout.showMsg(2, 123);
                                                                                tabLayout.showMsg(3, 1234);
                                                                                tabLayout.showDot(4);
                                                                                break;
                                                                            case "清空小圆点":
                                                                                tabLayout.hideMsg(0);
                                                                                tabLayout.hideMsg(1);
                                                                                tabLayout.hideMsg(2);
                                                                                tabLayout.hideMsg(3);
                                                                                tabLayout.hideMsg(4);
                                                                                break;
                                                                        }
                                                                    }
                                                                })
                                                        .show();
                                                break;
                                            case "Tab图标风格":
                                                new XPopup.Builder(getContext())
                                                        .isDarkTheme(true)
                                                        .popupAnimation(PopupAnimation.ScaleAlphaFromRightBottom)
                                                        .asCenterList("请选择一项", new String[]{"IOS", "OHOS"},
                                                                new OnSelectListener() {
                                                                    @Override
                                                                    public void onSelect(int position, String text) {
                                                                        switch (text){
                                                                            case "IOS":
                                                                                tabLayout.setFonttype(NiceTabLayout.FONTTYPE.Ios);
                                                                                break;
                                                                            case "OHOS":
                                                                                tabLayout.setFonttype(NiceTabLayout.FONTTYPE.OHOS);
                                                                                break;
                                                                        }
                                                                    }
                                                                })
                                                        .show();
                                                break;
                                        }
                                    }
                                })
                        .show();
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private class MyPagerAdapter extends PageSliderProvider {
        List<SamplePagerItem> titles;

        private MyPagerAdapter(List<SamplePagerItem> titles) {
            this.titles = titles;
        }

        @Override
        public int getCount() {
            if (titles == null) {
                return 0;
            } else {
                return titles.size();
            }
        }

        @Override
        public String getPageTitle(int position) {
            return titles.get(position).getTitle();
        }

        @Override
        public Object createPageInContainer(ComponentContainer componentContainer, int i) {
            Text text = new Text(getContext());
            text.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
            text.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
            text.setTextAlignment(TextAlignment.CENTER);
            text.setText(titles.get(i).getTitle());
            text.setTextColor(new Color(0xffff5454));
            text.setTextSize(30, Text.TextSizeType.VP);
            componentContainer.addComponent(text);
            return componentContainer;
        }

        @Override
        public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
            componentContainer.removeAllComponents();
        }

        @Override
        public boolean isPageMatchToObject(Component component, Object o) {
            return false;
        }
    }
    public void showToast(String str) {
        Context abilityContext= MainAbilitySlice.this;
        Text text = new Text(abilityContext);
        text.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
        text.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
        text.setTextSize(48);
        text.setText(str);
        text.setMultipleLine(true);
        text.setTextAlignment(TextAlignment.CENTER);
        DirectionalLayout directionalLayout = new DirectionalLayout(abilityContext);
        directionalLayout.setBackground(createDrawable(0xF0F0F0F0, 50));
        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig
                (DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        layoutConfig.setMarginBottom(100);
        directionalLayout.setLayoutConfig(layoutConfig);
        directionalLayout.setPadding(20, 30, 20, 30);
        directionalLayout.addComponent(text);
        ToastDialog toastDialog = new ToastDialog(abilityContext);
        toastDialog.setContentCustomComponent(directionalLayout).setDuration(2000).setAutoClosable(true)
                .setAlignment(LayoutAlignment.BOTTOM).setTransparent(true).show();
    }
    private static ShapeElement createDrawable(int color, float radius) {
        ShapeElement drawable = new ShapeElement();
        drawable.setShape(ShapeElement.RECTANGLE);
        drawable.setRgbColor(new RgbColor(color));
        drawable.setCornerRadius(radius);
        return drawable;
    }
}
